#include "app_common.h"
#include "log.h"
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <pthread.h>



//获取当前系统时间
long app_common_getcurtime_ms()
{
    struct timeval tv;
    
    gettimeofday(&tv, NULL);

    return (tv.tv_sec * 1000 + tv.tv_usec / 1000);


}

/**
 * @brief 把一个字节数组转换为16进制的字符串
 */
char *app_common_bytes2hexstr( char *bytes, int len)
{
    char *hexstr=malloc(len*2+1);
    for (size_t i = 0; i < len; i++)
    {
        sprintf(hexstr+i*2,"%02x",bytes[i]);
    }
    hexstr[len*2]='\0';
    return hexstr;
    

}


/**
 * @brief 把一个16进制的字符串转换为字节数组
 */
int  app_common_hexstr2bytes( char *hexstr, char *data_buff, int buff_len)
{
    int len=strlen(hexstr)/2;
    if(len>buff_len )
    {
        log_error("hexstr too long");
    }
    for (size_t i = 0; i < strlen(hexstr); i+=2)
    {
         sscanf(hexstr+i,"%2hhx",&data_buff[i/2]);
    }
    return len;

    


}


/**
 * @brief 把一个字符数组按16进制打印出来
 *
 */
 
void app_common_print_hex(char *hexstr, int len)
{
  
    for (size_t i = 0; i < len; i++)
    {
        printf("%02x ",hexstr[i]);
    }
   
    printf("\n");
}